package com.xbstar.web;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import javax.sql.DataSource;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/test")
public class Test {
	@Autowired
	DataSource dataSource;
	@RequestMapping("/test2")
	public  String test2(@RequestParam("name")String name) throws Exception{
		String sql="select * from User where name like ?";
		Connection conn=null;
		 conn = dataSource.getConnection();
		 PreparedStatement ps1 = conn.prepareStatement(sql);
		 ps1.setString(1, "%"+name+"%");
		 ResultSet rs1 = ps1.executeQuery();
		 
		 /**
		  * 自动生成@author cpudriver
		  * 获取字段名称
		  */
		 ResultSetMetaData rsmd = rs1.getMetaData();
		 int count=rsmd.getColumnCount();
		 String[] names_werertete=new String[count];
		 JSONArray result_qqqqq=new JSONArray();
		 for(int i=0;i<count;i++){
			 names_werertete[i]=rsmd.getColumnName(i+1);
		 }
		 while (rs1.next()) {
			 JSONObject ajson=new JSONObject();
			 for(int i=0;i<count;i++){
				 names_werertete[i]=rsmd.getColumnName(i+1);
				 ajson.put(names_werertete[i], names_werertete[i]);
			 }
			 result_qqqqq.put(ajson);

		 }
		 rs1.close();
		 ps1.close();
		return result_qqqqq.toString();
	}
}
